package com.sys.orders.head.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.pojo.ABlindbox;
import com.sys.goods.pojo.ABlindboxEnum;
import com.sys.goods.pojo.AGoods;
import com.sys.goods.pojo.AGoodsEnum;
import com.sys.goods.service.IGoodsDbService;
import com.sys.orders.pojo.AOrdersComment;
import com.sys.orders.pojo.AOrdersCommentEnum;
import com.sys.orders.service.IOrdersDbService;
import com.sys.users.pojo.ACartEnum;

/**
 * 商品
 * @author MyJjh
 *
 */
@Controller
@RequestMapping("/goods")
public class GoodsHeadController extends BaseController
{

	@Autowired
	private IGoodsDbService goodsDbService;
	@Autowired
	private IOrdersDbService ordersDbService;
	
	
	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @return 
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/goodsList")
	public String goodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("=========goodsList=========");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		pageInfoUtil.setPageSize(12);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 必须是上架的商品 */
		condMap.put("status", AGoodsEnum.STATUS_ENABLE.getStatus());
		
		/* 根据分类搜索 */
		String blindBoxId = request.getParameter("blindBoxId");
		if(blindBoxId == null)
		{
			blindBoxId = "" ; 
		}
		request.setAttribute("blindBoxId", blindBoxId);
		condMap.put("blindBoxId", blindBoxId);

		/* 根据dataType搜索 */
		String dataType = request.getParameter("dataType");
		if(dataType == null)
		{
			dataType = "" ; 
		}
		request.setAttribute("dataType", dataType);
		condMap.put("dataType", dataType);
		
		/* 根据分类搜索 */
		String orderby = request.getParameter("orderby");
		if(orderby == null)
		{
			orderby = "" ; 
		}
		request.setAttribute("orderby", orderby);
		condMap.put("orderby", orderby);
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AGoods> apiResponse = this.goodsDbService.findCondListAGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 查询所有的分类 */
		condMap.clear();
		condMap.put("status", ACartEnum.STATUS_ENABLE.getStatus());
		ApiResponse<ABlindbox> cateApiResponse = this.goodsDbService.findCondListABlindboxService(null, condMap);
		request.setAttribute("cateResponse", cateApiResponse.toJSON());
		/* 页面跳转 */
		return "/head/goodsList" ;
	}
	
	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/goodsInfo")
	public String goodsInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String blindBoxId = request.getParameter("blindBoxId");
		condMap.put("blindBoxId", blindBoxId);
		ApiResponse<ABlindbox> apiResponse = this.goodsDbService.findOneABlindboxService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		/* 加载评论列表 */
		condMap.clear();
		condMap.put("blindBoxId", blindBoxId);
		condMap.put("status", AOrdersCommentEnum.STATUS_ENABLE.getStatus());
		ApiResponse<AOrdersComment> commentApiResponse = this.ordersDbService.findCondListAOrdersCommentService(null, condMap);
		request.setAttribute("commentApiResponse", commentApiResponse.toJSON());
		/* 页面跳转 */
		return "/head/goodsInfo" ; 
	}
	
	/* ====================盲盒=========================== */
	/**
	 * 盲盒列表
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/boxList")
	public String boxList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);

		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<ABlindbox> apiResponse = this.goodsDbService.findCondListABlindboxService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());

		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());

		return "/head/goods/boxList";
	}
	
	/**
	 * 盲盒详情
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/boxInfo")
	public String boxInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String blindBoxId = request.getParameter("blindBoxId");
		condMap.put("blindBoxId", blindBoxId);
		ApiResponse<ABlindbox> apiResponse = this.goodsDbService.findOneABlindboxService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		condMap.clear();
		condMap.put("orderID", blindBoxId);
		/* 盲盒相关商品 */
		ApiResponse<AOrdersComment> commentApiResponse = this.ordersDbService.findCondListAOrdersCommentService(null, condMap);
		request.setAttribute("commentApiResponse", commentApiResponse.toJSON());
		/* 页面跳转 */
		return "/head/goods/boxInfo" ; 
	}

}
